package org.gangcai.mac.shop.activity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.zhihu.matisse.Matisse;

import org.greenrobot.eventbus.EventBus;
import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.CommonResonseBean;
import org.gangcai.mac.shop.bean.EventBusBaseBean1;
import org.gangcai.mac.shop.bean.PullAuthoBean;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.utils.MatisseUtils;
import org.gangcai.mac.shop.utils.UriUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.http.Multipart;

public class AuthoEducationActivity extends BaseAuthoActivity {

    @BindView(R.id.studentNum)
    EditText studentNum;
    @BindView(R.id.studentPwd)
    EditText studentPwd;
    @BindView(R.id.jietu)
    ImageView jietu;
    @BindView(R.id.nextStep)
    Button nextStep;
    private String realPathFromURI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        getAuthoData("getculture").subscribe(new CommonObserver<PullAuthoBean>() {
            @Override
            public void onSuccess(PullAuthoBean value) {
                loadImage(jietu, value.getInfo().getImg());
                studentPwd.setText(value.getInfo().getAccount());
                studentNum.setText(value.getInfo().getPwd());
            }
        });
    }

    @Override
    protected String getTitleName() {
        return "文化程度";
    }

    @Override
    public int getcontentView() {
        return R.layout.activity_autho_education;
    }

    @OnClick({R.id.jietu, R.id.nextStep})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.jietu:

                MatisseUtils.pickImage(this, 1, 100);
                break;
            case R.id.nextStep:
                if (realPathFromURI == null) {
                    ToastUtils.showShort("请选择图片");
                } else {
                    submitData();
                }
                break;
        }
    }

    private void submitData() {

        HashMap<String, String> paramsMap = new HashMap<>();
        HashMap<String, File> fileMap = new HashMap<>();
        File file = new File(realPathFromURI);


        fileMap.put("photo", file);

        paramsMap.put("uid", SPUtils.getInstance().getString("uid"));
        paramsMap.put("account", studentNum.getText().toString());
        paramsMap.put("pwd", studentPwd.getText().toString());
        prompDialog.showLoading("请等待");
        submitDataToServer("culture_ident", paramsMap, fileMap).subscribe(new CommonObserver<CommonResonseBean>(prompDialog) {
            @Override
            public void onSuccess(CommonResonseBean value) {
                if (value.getCode() == Constant.CODE_SUCC) {
                    showMsg();
                    EventBus.getDefault().postSticky(new EventBusBaseBean1());

                    finish();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            List<Uri> mSelected = Matisse.obtainResult(data);
            Glide.with(this).load(mSelected.get(0)).centerCrop().into(jietu);
            realPathFromURI = UriUtils.getRealPathFromURI(this, mSelected.get(0));
        }

    }


}
